package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ContractMapper;
import com.zzyl.nursing.domain.Contract;
import com.zzyl.nursing.service.IContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 合同Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-11
 */
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements IContractService
{
    @Autowired
    private ContractMapper contractMapper;

    /**
     * 查询合同
     * 
     * @param id 合同主键
     * @return 合同
     */
    @Override
    public Contract selectContractById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询合同列表
     * 
     * @param contract 合同
     * @return 合同
     */
    @Override
    public List<Contract> selectContractList(Contract contract)
    {
        return contractMapper.selectContractList(contract);
    }

    /**
     * 新增合同
     * 
     * @param contract 合同
     * @return 结果
     */
    @Override
    public int insertContract(Contract contract)
    {
        return save(contract) ? 1 : 0;
    }

    /**
     * 修改合同
     * 
     * @param contract 合同
     * @return 结果
     */
    @Override
    public int updateContract(Contract contract)
    {
        return updateById(contract) ? 1 : 0;
    }

    /**
     * 批量删除合同
     * 
     * @param ids 需要删除的合同主键
     * @return 结果
     */
    @Override
    public int deleteContractByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除合同信息
     * 
     * @param id 合同主键
     * @return 结果
     */
    @Override
    public int deleteContractById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 更新合同状态
     */
    public void myJob() {
        //0: 未生效, 1: 已生效, 2: 已过期, 3: 已失效
        List<Contract> list = lambdaQuery().le(Contract::getStatus, 1).
                select(Contract::getStatus, Contract::getStartDate, Contract::getEndDate, Contract::getId).list();
        LocalDateTime now = LocalDateTime.now();
        if(!list.isEmpty()){
            ArrayList<Contract> contracts = new ArrayList<>();
            Contract contract = new Contract();
            list.forEach(c->{
                if(c.getStartDate().isBefore(now)){
                    //已生效
                    if(c.getStatus()==0){
                        contract.setId(c.getId());
                        contract.setStatus(1);
                        contracts.add(contract);
                    }
                }else if(c.getEndDate().isBefore(now)){
                    //已过期
                    contract.setId(c.getId());
                    contract.setStatus(2);
                    contracts.add(contract);
                }
            });
            if(!contracts.isEmpty()){
                updateBatchById(contracts);
            }
        }
    }
}
